package Controler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-11-26
 * Time: 8:59
 * To BE A Better Person-2020-3-2
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先拿到session
        HttpSession session= req.getSession(false);
        if(session == null)
        {
            resp.setContentType("text/html;charset=utf8");

            resp.getWriter().write("请进行正确操作");
            resp.setStatus(302);
            resp.setHeader("Location","login.html");
            return;
        }
        session.removeAttribute("user");
        resp.setStatus(302);
        resp.setHeader("Location","login.html");
    }
}
